//
//  LoginViewController.m
//  老司机发车了
//
//  Created by 斌 on 16/8/6.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
@interface LoginViewController ()

// 用户名
@property (weak, nonatomic) IBOutlet UITextField *userName;
// 密码
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UIImageView *backimg;


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登陆";
    self.backimg.image = [UIImage imageNamed:@"登陆界面.jpg"];
    self.userName.text = _name;
    self.password.text = _pass;
}

// 登陆按钮方法
- (IBAction)loginAction:(id)sender {
    
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"User"];
    bquery.limit = 1000;
    // 添加约束条件
    [bquery whereKey:@"username" equalTo:_userName.text];
    //查找GameScore表的所有符合约束条件的数据
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        for (BmobObject *obj in array) {
            //打印playerName
            NSLog(@"obj.playerName = %@", [obj objectForKey:@"username"]);
            NSLog(@"%@",[obj objectForKey:@"password"]);
            
            // 验证用户名和密码是否匹配
            if ([[obj objectForKey:@"password"] isEqualToString:_password.text]) {
                NSLog(@"登陆成功");
                
                // 登陆成功赋值用户信息模型
                [macro shareInstance].userInfo.objectId = [obj objectForKey:@"objectId"];
                [macro shareInstance].userInfo.imgUrl = [obj objectForKey:@"imgUrl"];
                [macro shareInstance].userInfo.nickname = [obj objectForKey:@"nickname"];
                [macro shareInstance].userInfo.level = [obj objectForKey:@"level"];
                [macro shareInstance].userInfo.sign = [obj objectForKey:@"sign"];
                [macro shareInstance].userInfo.phoneNum = [obj objectForKey:@"phoneNum"];
                [macro shareInstance].userInfo.username = [obj objectForKey:@"username"];
                [macro shareInstance].userInfo.password = [obj objectForKey:@"password"];
                
                
                // 登陆状态为yes
                [macro shareInstance].isLogin = YES;
                
                // 发送通知
                [[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:nil];
                
                //添加字典
                NSDictionary *dict =@{@"imgUrl":[macro shareInstance].userInfo.imgUrl,@"nickname":[macro shareInstance].userInfo.nickname,@"level":[macro shareInstance].userInfo.level,@"sign":[macro shareInstance].userInfo.sign};
                //创建通知
                NSNotification *notification =[NSNotification notificationWithName:@"tongzhi" object:nil userInfo:dict];
                //通过通知中心发送通知传值
                [[NSNotificationCenter defaultCenter] postNotification:notification];
                
                [self dismissViewControllerAnimated:YES completion:nil];
                
                
            } else {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                [alert addAction:sure];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }
        NSLog(@"%lu",(unsigned long)array.count);
    }];
}

// 注册按钮方法
- (IBAction)registerAction:(id)sender {
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
    
}

// 返回按钮方法
- (IBAction)backAction:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
